<template>
  <div class="directorys-container">
    <el-card class="box-card" is-always-shadow>
      <!-- 输入框 以及功能操作区域 -->
      <el-row>
        <el-col :span="18">
          <el-form :inline="true" :model="queryInfo" ref="dirFormRef" class="demo-form-inline"
            v-loading="loading">
            <el-form-item label="目录名称" prop="directoryName">
              <el-input size="small" clearable @clear="getdirList"
                v-model.trim="queryInfo.directoryName" @keyup.enter="getdirList"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="state" class="form-use">
              <el-select v-model="queryInfo.state" placeholder="请选择" size="small">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="onClear">清空</el-button>
              <el-button type="primary" @click="getdirList" size="small">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-button style="float: right; padding: 10px" type="success" icon="el-icon-edit"
            size="small" @click="addDirect">新增目录</el-button>
        </el-col>
      </el-row>
      <!-- / 输入框 以及功能操作区域 -->

      <!-- 消息提示区域 -->
      <el-row>
        <el-alert type="info" show-icon :closable="false" class="alret">
          <div slot="title">共 {{ total }} 条记录</div>
        </el-alert>
      </el-row>
      <!--/ 消息提示区域 -->

      <!-- 数据操作 -->
      <el-table :data="directList" stripe style="width: 100%">
        <el-table-column type="index" label="序号" width="80px"></el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="directoryName" label="目录名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者"></el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template slot-scope="scope">
            {{ scope.row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"></el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            <div>{{ scope.row.state === 1 ? "已启用" : "已禁用" }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="state" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="changeState(scope.row)">
              <div>{{ scope.row.state === 0 ? "启用" : "禁用" }}</div>
            </el-button>
            <el-button type="text" :disabled="scope.row.state === 1"
              @click="editRedirect(scope.row)">修改</el-button>
            <el-button type="text" :disabled="scope.row.state === 1"
              @click="deleteRedirect(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 数据操作 -->

      <!-- 分页 -->
      <!-- 分页导航区域
      @size-change(pagesize改变时触发)
      @current-change(页码发生改变时触发)
      :current-page(设置当前页码)
      :page-size(设置每页的数据条数)
      :total(设置总页数) -->
      <el-pagination background class="page" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page.sync="queryInfo.pagenum"
        :page-sizes="[2, 5, 10]" :page-size="queryInfo.pagesize"
        layout="prev, pager, next, sizes, jumper" :total="total">
      </el-pagination>
      <!-- / 分页 -->
    </el-card>

    <!-- 添加对话框 -->
    <el-dialog title="新增目录" :visible.sync="dialogAddShow" width="400px" @close="handleClose"
      class="dialogAdd">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px"
        class="demo-ruleForm">
        <el-form-item label="所属学科" size="small" prop="subjectID">
          <el-select v-model="addForm.subjectID" placeholder="请选择所属学科">
            <el-option v-for="item in subjectList" :key="item.id" :label="item.subjectName"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" size="small" prop="directoryName">
          <el-input v-model="addForm.directoryName" placeholder="请输入目录名称"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogAddShow = false">取 消</el-button>
        <el-button type="primary" @click="dialogAddForm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- /添加对话框 -->

    <!-- 修改对话框  -->
    <el-dialog title="修改目录" :visible.sync="dialogEditShow" width="400px" class="dialogAdd"
      @close="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px"
        class="demo-ruleForm">
        <el-form-item label="所属学科" size="small" prop="subjectID">
          <el-select v-model="editForm.subjectID">
            <el-option v-for="item in subjectList" :key="item.id" :label="item.subjectName"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" size="small" prop="directoryName">
          <el-input v-model="editForm.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogEditShow = false">取 消</el-button>
        <el-button type="primary" @click="dialogEditForm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- / 修改对话框  -->
  </div>
</template>

<script>
import { parseTimeByString } from '@/filters'
import { list, add, changeState, update, remove } from '@/api/hmmm/directorys'
// 因为es6不允许重复的 导入的项 只能用之前的来代替了
// const { list: subjects } = require('../../api/hmmm/subjects')
import { list as subjects } from '@/api/hmmm/subjects'

export default {
  name: 'subject-directory',
  components: {},
  props: {},
  data () {
    return {
      // 获取学科列表数据
      subjectList: [],
      // 获取目录列表数据
      directList: [],
      // 获取查询用户信息的参数
      queryInfo: {
        // 当前的页数，即页码
        page: 1,
        // 每页显示的数据条数
        pagesize: 10,
        subjectID: null,
        directoryName: null,
        state: null
      },
      // 总数据条数
      total: 0,
      // 弹出层显示与隐藏
      dialogAddShow: false,
      // 添加表单数据对象
      addForm: {
        directoryName: '',
        subjectID: ''
      },
      // 修改表单数据对象
      editForm: {
        subjectID: '',
        directoryName: ''
      },
      // 验证添加表单的规则
      addFormRules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
        ]
      },
      // 修改表单的验证规则
      editFormRules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
        ]
      },
      // 加载默认状态
      loading: false,
      // 修改弹出层显示与隐藏
      dialogEditShow: false,
      // 修改获取的id
      data_id: ''
    }
  },
  computed: {},
  watch: {},
  created () {
    // 获取学科列表
    this.subjectLists()
    // 获取目录列表
    this.getdirList()
  },
  mounted () { },
  methods: {
    // 请求接口 获取目录列表数据
    async getdirList () {
      try {
        // 加载load开始
        this.loading = true
        const { data } = await list(this.queryInfo)
        this.directList = data.items
        this.total = data.counts
        // 加载load关闭
        this.loading = false
        // 提示成功
        this.$message.success('获取列表成功')
      } catch (err) {
        // 加载load关闭
        this.loading = false
        // 提示失败
        this.$message.fail('获取列表失败')
      }
    },
    // 更新当前页数
    handleSizeChange (newSize) {
      // pagesize改变时触发，当pagesize发生改变的时候，我们应该
      // 以最新的pagesize来请求数据并展示数据
      this.queryInfo.pagesize = newSize
      // 重新按照pagesize发送请求，请求最新的数据
      this.getdirList()
    },
    // 更新当前页面
    handleCurrentChange (current) {
      // 页码发生改变时触发当current发生改变的时候，我们应该
      // 以最新的current页码来请求数据并展示数据
      this.queryInfo.page = current
      // 重新按照pagenum发送请求，请求最新的数据
      this.getdirList()
    },
    // 获取学科列表数据
    async subjectLists () {
      try {
        const { data } = await subjects()
        this.subjectList = data.items
      } catch (err) {
        this.$message.fail('获取学科列表失败')
      }
    },
    // 清空输入框和 下拉框
    onClear () {
      this.$refs.dirFormRef.resetFields()
      this.getdirList()
    },
    // 点击按钮弹出显示层
    addDirect () {
      // 弹出层显示
      this.dialogAddShow = true
    },
    // 重置表单数据
    handleClose () {
      this.$refs.addFormRef.resetFields()
    },
    // 确认提交表单 添加
    dialogAddForm () {
      // 点击确定按钮，添加新用户
      // 调用validate进行表单验证
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        try {
          // 加载load开启
          this.loading = true
          await add(this.addForm)
          // 加载load关闭
          this.loading = false
          // 提示消息
          this.$message.success('添加成功')
          // 关闭弹出层
          this.dialogAddShow = false
          // 更新渲染
          this.getdirList()
        } catch (err) {
          // 失败提示
          this.$message.fail('添加失败')
          // 加载load关闭
          this.loading = false
        }
      })
    },
    // 点击 切换状态
    async changeState (set) {
      // set.state = !set.state
      try {
        await changeState({
          id: set.id,
          state: set.state === 0 ? 1 : 0
        })
        // 判定状态 0 的话变成1 或者0
        set.state = set.state === 0 ? 1 : 0
        this.$message.success('切换成功')
      } catch (err) {
        this.$message.fail('切换失败')
      }
    },
    // 点击 修改按钮 获取数据
    editRedirect (row) {
      // 获取这行的数据
      this.data_id = row.id
      this.editForm.subjectID = row.subjectID
      this.editForm.directoryName = row.directoryName
      // 显示弹出层
      this.dialogEditShow = true
    },
    // 点击对话框按钮 修改数据
    dialogEditForm () {
      // 提交之前预验证提交之前的数据是否合法(如果开始用户没填写直接登录打回去)
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        try {
          // 加载开始
          this.loading = true
          // 发送请求
          await update({
            subjectID: this.editForm.subjectID,
            directoryName: this.editForm.directoryName,
            id: this.data_id
          })
          // 关闭加载
          this.loading = false
          // 更新渲染页面
          this.getdirList()
          // 成功的提示
          this.$message.success('修改成功')
          // 弹出层隐藏
          this.dialogEditShow = false
        } catch (err) {
          // 弹出层隐藏
          this.dialogEditShow = false
          // 关闭加载
          this.loading = false
          this.$message.fail('修改失败')
        }
      })
    },
    // 修改参数后重置里面的参数
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 删除功能
    async deleteRedirect (id) {
      // 弹出消息提示框
      // 弹出消息提示框
      const confirmResult = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // 返回的不是'confirm' 就取消删除
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      try {
        // 加载状态开启
        this.loading = true
        await remove({ id })
        // 加载状态关闭
        this.loading = false
        this.$message.success('删除成功')
        // 更新页面
        this.getdirList()
      } catch (err) {
        // 加载状态关闭
        this.loading = false
        this.$message.fail('删除失败')
      }
    }
  },
  filters: {
    parseTimeByString
  }
}
</script>

<style lang="scss" scoped>
::v-deep.directorys-container {
  .dialogAdd .el-dialog__footer {
    text-align: right !important;
  }
  padding: 10px;
  .box-card {
    padding: 10px;
    .form-use {
      margin-left: 20px;
    }
    .alret {
      margin-bottom: 15px;
    }
    .page {
      margin-top: 20px;
    }
    .el-pagination {
      float: right;
    }
  }
}
</style>
